package com.sfdk.core
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.utils.getDefinitionByName;

/**
	 * To be used with the following syntax :
	 * [Frame(factoryClass="com.sfdk.core.BasicPreloader")]
	 */
	 
	public class BasicPreloader extends MovieClip
	{
		public function BasicPreloader()
		{
			stop();
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
		}

		/**
		 * Loading the main class
		 */
		private function init() : void
		{
			var mainClass : Class = Class(getDefinitionByName("GameMain"));
			if(mainClass)
			{
				var app : Object = new mainClass();
				addChild(app as DisplayObject);
			}
		}
		
		/* Event handler */
		private function onEnterFrame(event : Event) : void
		{
			graphics.clear();
			if(framesLoaded == totalFrames)
			{
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				nextFrame();
				init();
			}
			else
			{
				var percent : Number = root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal;
				
				graphics.beginFill(0);
				graphics.drawRect(0, stage.stageHeight / 2 - 10,
				stage.stageWidth * percent, 20);
				graphics.endFill();
			}
		}		
	}
}